require.config({
	paths:{
		'jquery':'jquery',
		'cookie':'jquery.cookie'
	}
})
require(['jquery','cookie'],function($,cookie){
	$(function(){
		//获取所有的小图片
		var smaImg = $('.smallimg li img');
		//获取所有的大图
		var bigImg = $('.bigimg img');
		//定义一个下标
		var cindex = 0;
		//定义一个初始zindex值
		var zIndex = 1;
		//console.log(smaImg.length);
		for(var i = 0;i < smaImg.length;i ++){
			smaImg[i].index = i;
			smaImg[i].onmousemove = function(){
				cindex = this.index;
				bigImg[cindex].style["z-index"] = ++ zIndex;
				$('.largeimg img').attr('src',bigImg[cindex].src);
			}
		}
		//鼠标跟随
		//console.log($('.jlg_details_box .bigimg').width())
		$('.jlg_details_box .bigimg').mousemove(function(e){
			e = e || window.event;
			var l = e.clientX - $('.jlg_details_box').offset().left - $('.filter').width() / 2;
			var t = e.clientY - $('.jlg_details_box').offset().top - $('.filter').height() / 2;
			//边界处理
			l = l < 0 ? 0 : (l >  $('.jlg_details_box .bigimg').width() - $('.filter').width() ?  $('.jlg_details_box .bigimg').width() - $('.filter').width() : l);
			t = t < 0 ? 0 : (t >  $('.jlg_details_box .bigimg').height() - $('.filter').height() ?  $('.jlg_details_box .bigimg').height() - $('.filter').height() : t);
			$('.filter').css({'top':t + 'px','left':l + 'px'});	
			//放大镜效果
			//滤镜移动比例
			var fX = l / ($('.jlg_details_box .bigimg').width() - $('.filter').width());
			var fY = t / ($('.jlg_details_box .bigimg').height() - $('.filter').height());
			//大图放大比例
			var largeX = -fX * ($('.largeimg img').width() - $('.largeimg').width());
			var largeY = -fY * ($('.largeimg img').height() - $('.largeimg').height());
			//console.log(largeX,largeY)
			$('.largeimg img').css({'top':largeY + 'px','left':largeX + 'px'});
		})	
		
		//加入购物车效果
		loadCart();
		//给购物车添加一个点击事件
		$('.jlg_cart').click(function(){
			location.href = 'cart.html';
		})
		//给增加按钮添加事件
		var num = 1;
		$('.add').click(function(){			
			num ++;
			$('.num2txt').val(num);
		})
		//给减少按钮添加事件
		$('.jian').click(function(){			
			if($('.num2txt').val() > 1){
				num --;
			}
			$('.num2txt').val(num);
		})
		//给文本框添加失焦事件
		$('.num2txt').blur(function(){
			var re = /^\d+$/;
			if(re.test($(this).val()) == false || $('.num2txt').val() < 1){
				$('.num2txt').val('1');
			}
		})
		//给加入购物车添加点击事件
		$('.joincart').click(function(){
			var goodID = $('.jieshao').attr('data-good-id');
			var goodName = $('.wenzi b').html();
			var goodPrice = parseFloat($('.pn b').html());
			var goodSrc = $('.largeimg img').attr("src");
			var cartStr = $.cookie('cart') ? $.cookie('cart') : '';
			//将字符串转化为对象
			var cartObj = convertCartStrToObj(cartStr);
			if(goodID in cartObj){
				cartObj[goodID].num += parseInt($('.num2txt').val());
			}else{
				cartObj[goodID] = {
					name : goodName,
					price : goodPrice,
					num : parseInt($('.num2txt').val()),
					src : goodSrc
				};
			}
			//将创造的对象转换为字符串存入cookie中
			cartStr = convertObjToCartStr(cartObj);
			//存入cookie
			$.cookie('cart',cartStr,{expires:7,path:'/'});
			loadCart();
		})
		
		//将字符串转为对象
		function convertCartStrToObj(cartStr){
			if(!cartStr){
				return {};
			}
			var goods = cartStr.split(':');
			var obj = {};
			for(var i = 0;i < goods.length;i ++){
				var data = goods[i].split(',');
				obj[data[0]] = {
					name : data[1],
					price : parseFloat(data[2]),
					num : parseInt(data[3]),
					src : data[4]					
				}
			}
			return obj;
		}
		//将对象转换为字符串
		function convertObjToCartStr(obj){
			var cartStr = '';
			for(var id in obj){
				if(cartStr){
					cartStr += ':';
				}
				cartStr += id + ',' + obj[id].name + "," + obj[id].price + ',' + obj[id].num + ',' + obj[id].src;
			}
			return cartStr;
		}
		//加载购物车信息
		function loadCart(){
			var cartStr = $.cookie('cart') ? $.cookie('cart') : '';
			var cartObj = convertCartStrToObj(cartStr);
			var total = 0;
			for(var id in cartObj){
				total += cartObj[id].num;;
			}
			console.log(cartObj[id])
			$('.count').html(total);
		}
	})
})